﻿/*-----------------------------------------------
// File: LegacyAnimation.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//[RequireComponent(typeof(ModelRenderKit))]
public class LegacyAnimation : MonoBehaviour
{
    public Animation anim;
    public string modelName;

    public float GetAniLength(string aniname)
    {
        if (anim.GetClip(aniname) != null)
            return anim.GetClip(aniname).length;
        return 0f;
    }

    public void Play(string aniName, WrapMode mode, bool rewind = false, float speed = 1f)
    {
        if (anim.GetClip(aniName) != null)
        {
            anim.wrapMode = mode;
            anim[aniName].speed = speed;
            if (anim.IsPlaying(aniName))
            {
                if (rewind)
                {
                    anim.Rewind(aniName);
                    anim.Play(aniName);
                    currentAniName = aniName;
                }
            }
            else
            {
                anim.Play(aniName);
                currentAniName = aniName;
            }

            var kit = GetComponent<ModelRenderKit>();
            if (kit != null)
            {
                kit.PlayAnimation(aniName, mode, rewind, speed);
            }
        }
        else
        {
            Debug.LogError("[LegacyAnimation] PlayAnimation: " + aniName + " not exist in " + anim.gameObject.name);
        }
    }
    public void CrossFade(string aniName, WrapMode mode, bool rewind = false, float fadeTime = 0.3f)
    {
       
        if (anim.GetClip(aniName) != null)
        {
            anim.wrapMode = mode;
            //anim[aniName].speed = speed;
            if (anim.IsPlaying(aniName))
            {
                if (rewind)
                {
                    anim.Rewind(aniName);
                    anim.CrossFade(aniName);
                    currentAniName = aniName;
                }
            }
            else
            {
                anim.CrossFade(aniName, fadeTime);
                currentAniName = aniName;
            }
        }
        else {
            Debug.LogError("[LegacyAnimation] CrossFadeAnimation: " + aniName + " not exist in " + anim.gameObject.name);
        }
    
    }

    public void Stop() {
        anim.Stop();
    }


    public void PlayQueue(string aniName)
    {
        anim.PlayQueued(aniName);
    }
    public void CrossFaceQueue(string aniName)
    {
        anim.CrossFadeQueued(aniName);
    }

    public bool IsPlaying(string aniName)
    {
        return anim.IsPlaying(aniName);
    }

    public bool isPlaying
    {
        get
        {
            return anim.isPlaying;
        }
    }

    public string _currentAniName;
    public string currentAniName
    {
        get { return _currentAniName; }
        set {
            _currentAniName = value;
        }
    }


    public ModelCommonExpressType currentExpressType;

    bool isForceFace = false;
    public void EnterForceFace()
    {
        isForceFace = true;
    }

    public void ResetFace()
    {
        isForceFace = false;
        GetComponent<ModelRenderKit>().SetDefaultFace();
    }

    public bool ChangeCommonFace(string faceId)
    {
        var dict = Dict.Blo.DictHeroBlo.GetTableHeroEmojiRes(faceId);
        if (dict != null)
        {
            GetComponent<ModelRenderKit>().SetFace(dict.FaceName);
            return true;
        }
        else
        {
            GetComponent<ModelRenderKit>().SetDefaultFace();
            return false;
        }
    }

    bool ChangeCommonFace(ModelExpressDetail type) {
        var dict = Dict.Blo.DictHeroBlo.GetTableHeroEmojiResByModelNameAndExpressType(modelName, type);
        if (dict != null)
        {
            GetComponent<ModelRenderKit>().SetFace(dict.FaceName);
            return true;
        }
        else
        {
            GetComponent<ModelRenderKit>().SetDefaultFace();
            return false;
        }
    }

    IEnumerator Blink() {
        if (!ChangeCommonFace(ModelExpressDetail.CLOSE_EYE)) {
            yield break;
        }
        yield return new WaitForSeconds(1f / 30f * 3);
        ResetFace();
    }

    public string currentFaceId;

    float nextBlinkTime = 0f;
    float currentBlinkTime = 0f;

    void Start()
    {
        ResetFace();
    }

    void Update() {
        if (!isForceFace)
        {
            currentBlinkTime += Time.deltaTime;
            if (currentBlinkTime >= nextBlinkTime)
            {
                StartCoroutine(Blink());
                currentBlinkTime = 0f;
                nextBlinkTime = Random.Range(5f, 15f);
            }
        }
    }

}
